import type { ApiResponse, AddFriendRequest, FriendRequestData } from "@/types";
import { get, post, put, del } from "@/utils/request";

/**
 * 发送好友请求
 * @param params 请求参数，包含好友ID和可选的备注
 * @returns Promise<ApiResponse<void>> 返回一个Promise，成功时data为void
 */
export const sendFriendRequest = async (
  params: AddFriendRequest
): Promise<ApiResponse<void>> => {
  return post("/contacts/requests", params);
};

/**
 * 获取收到的好友请求列表
 * @returns Promise<ApiResponse<FriendRequestData[]>> 返回一个Promise，成功时data为好友请求列表
 */
export const getReceivedFriendRequests = async (): Promise<
  ApiResponse<FriendRequestData[]>
> => {
  return get("/contacts/requests/received");
};

/**
 * 获取发送的好友请求列表
 * @returns Promise<ApiResponse<FriendRequestData[]>> 返回一个Promise，成功时data为好友请求列表
 */
export const getSentFriendRequests = async (): Promise<
  ApiResponse<FriendRequestData[]>
> => {
  return get("/contacts/requests/sent");
};

/**
 * 处理好友请求
 * @param requestId 请求ID
 * @param status 处理状态 1: 接受, 2: 拒绝
 * @returns Promise<ApiResponse<void>> 返回一个Promise，成功时data为void
 */
export const handleFriendRequest = async (
  requestId: string,
  status: number
): Promise<ApiResponse<void>> => {
  return put(`/contacts/requests/${requestId}`, { status });
};

/**
 * 获取好友列表
 * @returns Promise<ApiResponse<FriendRequestData[]>> 返回一个Promise，成功时data为好友列表
 */
export const getFriendList = async (): Promise<
  ApiResponse<FriendRequestData[]>
> => {
  return get("/contacts/friends");
};

/**
 * 删除好友
 * @param friendId 好友ID
 * @returns Promise<ApiResponse<void>> 返回一个Promise，成功时data为void
 */
export const deleteFriend = async (
  friendId: string
): Promise<ApiResponse<void>> => {
  return del(`/contacts/friends/${friendId}`);
};

/**
 * 更新好友备注
 * @param friendId 好友ID
 * @param remark 新的备注
 * @returns Promise<ApiResponse<void>> 返回一个Promise，成功时data为void
 */
export const updateRemark = async (
  friendId: string,
  remark: string
): Promise<ApiResponse<void>> => {
  return put(
    `/contacts/friends/${friendId}/remark?remark=${encodeURIComponent(remark)}`
  );
};
